<?php

namespace App\Imports;


use App\Models\dispatch\MaterialModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class PurchaseImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];


        foreach ($rows as $k => $row) {
            if($k < 2) continue;

            $find = MaterialModel::where('code', $row[6])->where('name', $row[7])->first();

            if(!$find) {
                $this->result->error[] = '物料' . $row[7] . ' 不存在';
                continue;
            }

            $lines[] = [
                'apply_date' => $row[1],
                'apply_dept' => $row[2],
                'apply_person' => $row[3],
                'reason' => $row[4],
                'category' => $row[5],
//                'material_code' => $row[6],
//                'material_name' => $row[7],
                'material_id' => $find->id,
                'spe' => $row[8],
                'model' => $row[9],
                'color' => $row[10],
                'unit' => $row[11],
                'number' => $row[12],
                'price' => $row[13],
                'total' => $row[14],
                'channel' => $row[15],
                'last_number' => $row[16],
                'last_time' => $row[17],
                'pre_date' => $row[18],
                'status' => $row[19],
                'remark' => $row[20],
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
